# -*- coding: utf-8 -*-

import pytest
import time
from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_service import AppiumService

# 开启服务端
APPIUM_HOST = '127.0.0.1'
APPIUM_PORT = 4723
@pytest.fixture(scope="session")
def start_appium_service():
    server = AppiumService()
    server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
    yield server
    server.stop()

# 创建客户端到服务端的会话
def create_appium_session_by_api(custom_opts = None, appium_host = APPIUM_HOST, appium_port = APPIUM_PORT):
    options = UiAutomator2Options()
    if custom_opts is not None:
        options.load_capabilities(custom_opts)
    return webdriver.Remote(f'http://{appium_host}:{appium_port}', options=options)


def test_operate_gms_call(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)

    driver.make_gsm_call("13888888888", "call")

    driver.quit()

# !检测appu是否已经安装
def test_check_if_app_installed(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)

    assert driver.is_app_installed(driver.current_package) == True

    driver.quit()

# !使用本地路径安装APP
def test_install_app_by_localfile(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"
    #assert driver.is_app_installed(ContactManagerAppId) == False
    #driver.push_file(destination_path="/sdcard/Download/ContactManager.apk", source_path="/home/shengyi/download/ContactManager.apk")

    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    assert driver.is_app_installed(ContactManagerAppId) == True

    driver.start_activity(ContactManagerAppId, ".ContactManager")
    assert driver.current_package == ContactManagerAppId

    driver.quit()

# !使用远程url路径安装APP
def test_install_app_by_remote_url(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    driver.install_app("https://github.com/appium/sample-apps/raw/master/pre-built/ContactManager.apk", grantPermissions=True)
    assert driver.is_app_installed(ContactManagerAppId) == True

    driver.start_activity("com.example.android.contactmanager", ".ContactManager")
    assert driver.current_package == ContactManagerAppId

    driver.quit()


# !使用Capability参数初始化安装APP
def test_install_app_by_capability(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
        "appium:app": "/tmp/ContactManager.apk",
        "appium:autoGrantPermissions": True,
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    assert driver.is_app_installed(ContactManagerAppId) == True
    driver.start_activity("com.example.android.contactmanager", ".ContactManager")
    assert driver.current_package == ContactManagerAppId

    driver.quit()

# !启动APP
def test_activate_app(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)

    # 先安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)

    ContactManagerAppId = "com.example.android.contactmanager"
    driver.activate_app(ContactManagerAppId)
    time.sleep(1) # 延时1S等待应用启动
    assert driver.current_package == ContactManagerAppId

    driver.quit()

# !卸载APP
def test_remove_app(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    # 先安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    assert driver.is_app_installed(ContactManagerAppId) == True

    driver.remove_app(ContactManagerAppId) # 移除安装的应用
    assert driver.is_app_installed(ContactManagerAppId) == False

    driver.quit()

# !结束APP
def test_terminate_app(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    # 先安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    assert driver.is_app_installed(ContactManagerAppId) == True

    # 启动
    driver.activate_app(ContactManagerAppId)
    time.sleep(1) # 延时1S等待应用启动
    assert driver.current_package == ContactManagerAppId

    driver.terminate_app(ContactManagerAppId) # 结束应用
    assert driver.current_package != ContactManagerAppId

    driver.quit()



# !启动activity
def test_start_activity(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    # 先安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    
    driver.start_activity("com.example.android.contactmanager", ".ContactManager")
    assert driver.current_activity == ".ContactManager"

    driver.quit()


# !查询APP运行状态
def test_query_app_state(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    # 卸载
    driver.remove_app(ContactManagerAppId)
    assert driver.query_app_state(ContactManagerAppId) == 0

    # 安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    assert driver.query_app_state(ContactManagerAppId) == 1
    
    # 启动
    driver.start_activity(ContactManagerAppId, ".ContactManager")
    assert driver.query_app_state(ContactManagerAppId) == 4

    driver.quit()


# !将APP置于后台运行
def test_put_app_to_background(start_appium_service):
    custom_opts = {
        "appium:avd": "testPhone",
    }

    driver = create_appium_session_by_api(custom_opts)
    ContactManagerAppId = "com.example.android.contactmanager"

    # 安装
    driver.install_app("/tmp/ContactManager.apk", grantPermissions=True)
    assert driver.query_app_state(ContactManagerAppId) == 1
    
    # 启动
    driver.start_activity(ContactManagerAppId, ".ContactManager")
    assert driver.query_app_state(ContactManagerAppId) == 4

    # 置于后台运行3秒
    driver.background_app(3)

    time.sleep(5)
    # background_app接口会阻塞，超时时间到了之后会恢复到前台运行
    assert driver.query_app_state(ContactManagerAppId) == 4


    # 置于后台运行，负数时间，不会恢复到前台了
    driver.background_app(-1)
    time.sleep(10)
    assert driver.query_app_state(ContactManagerAppId) == 3

    driver.quit()